﻿/********************************************************
 * Module Name    : Show Tree(menu and favorite) 
 * Purpose        : used to Get Tree Node Data(eg menu , org etc) 
 * Class Used     : GlobalVariable.cs, TreeNode(System default)
 * Created By     : Harwinder 
 * Date           : 24 nov 2008
**********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;

using System.Collections;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace VAdvantage.Classes
{
    public class VTreeNode : TreeViewItem
    {

        public enum ContextEventType
        {
            Add,
            Favourite,
            Move,
            Insert,
            Cancel
        }

        #region "Declare Variable"
        /** Node ID         */
        private int _Node_ID;
        /**	SeqNo			*/
        private int _seqNo;
        /** Name			*/
        private string _name;
        /** Description		*/
        private string _description;
        /**	Parent ID		*/
        private int _Parent_ID;
        /**	Summaty			*/
        private bool _isSummary;
        /** Image Indicator				*/
        private string _imageIndicator;
        /** Window ID       */
        private int _AD_Window_ID;
        /** Process ID      */
        private int _AD_Process_ID;
        /** Form ID         */
        private int _AD_Form_ID;
        /** Workflow ID     */
        private int _AD_Workflow_ID;
        /** Task ID         */
        private int _AD_Task_ID;
        /** Workbench ID    */
        private int _AD_Workbench_ID;
        /** Index to Icon               */
        private int _imageIndex = 0;
        /**	On Bar			*/
        private bool _onBar;

        /**	On Bar			*/
        private bool _isEditable;
        /**	Color			*/
        //private color m_color;

        public static String ACTION_Workbench = "B";
        /** WorkFlow = F */
        public static String ACTION_WorkFlow = "F";
        /** Process = P */
        public static String ACTION_Process = "P";
        /** Report = R */
        public static String ACTION_Report = "R";
        /** Task = T */
        public static String ACTION_Task = "T";
        /** Window = W */
        public static String ACTION_Window = "W";
        /** Form = X */
        public static String ACTION_Form = "X";
            //public const  String ACTION_UserChoice = "C";
            //public const String ACTION_SetVariable = "V";
            //public const String ACTION_DocumentAction = "D";
        /*************************************************************************/

            /**	Window - 1			*/
            public static int TYPE_WINDOW = 1;
            /**	Report - 2			*/
            public static int TYPE_REPORT = 2;
            /**	Process - 3			*/
            public static int TYPE_PROCESS = 3;
            /**	Workflow - 4		*/
            public static int TYPE_WORKFLOW = 4;
            /**	Workbench - 5		*/
            public static int TYPE_WORKBENCH = 5;
            /**	Variable - 6		*/
            public static int TYPE_SETVARIABLE = 6;
            /**	Choice - 7			*/
            public static int TYPE_USERCHOICE = 7;
            /**	Action - 8			*/
            public static int TYPE_DOCACTION = 8;


        /**************************************************************************/

            public static String ACTION_UserWorkbench = "B";
            /** User Choice = C */
            public static String ACTION_UserChoice = "C";
            /** Document Action = D */
            public static String ACTION_DocumentAction = "D";
            /** Sub Workflow = F */
            public static String ACTION_SubWorkflow = "F";
            /** EMail = M */
            public static String ACTION_EMail = "M";
            /** Apps Process = P */
            public static String ACTION_AppsProcess = "P";
            /** Apps Report = R */
            public static String ACTION_AppsReport = "R";
            /** Apps Task = T */
            public static String ACTION_AppsTask = "T";
            /** Set Variable = V */
            public static String ACTION_SetVariable = "V";
            /** User Window = W */
            public static String ACTION_UserWindow = "W";
            /** User Form = X */
            public static String ACTION_UserForm = "X";
            /** Wait (Sleep) = Z */
            public static String ACTION_WaitSleep = "Z";

        /********************************************************************/

            private Panels.IStartMenuTree _handler = null;

            private ContextMenu contextMenu;
            private MenuItem menuItem;

            public MenuItem CopyItem = null;
            public MenuItem PasteItem = null;
            public MenuItem CancelItem = null;
        private static string strRemove="RemoveFromFavorite";
        private static string strAdd = "AddToFavorite";
      
             static VTreeNode()
            {
               strRemove= Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "RemoveFromFavorite");
               strAdd = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "AddToFavorite"); 
            }


             public VTreeNode(string imageIndicator, string name)
             {
                 _imageIndicator = imageIndicator;
                 _name = name;

                 StackPanel panel = new StackPanel();
                 panel.Orientation = Orientation.Horizontal;
                 imgHeader = new Image();
                 imgHeader.Source = Utility.Envs.LoadImageSource(imageIndicator);
                 imgHeader.Width = 16;
                 imgHeader.Height = 16;
                 panel.Children.Add(imgHeader);
                 panel.Children.Add(new TextBlock() { Text = name, Margin = new System.Windows.Thickness(5, 0, 0, 0) });
                 this.Header = panel;
                 //this.Name = "Name" + index.ToString();
             }

             public VTreeNode(string imageIndicator, string name, int index)
             {
                 _imageIndicator = imageIndicator;
                 _name = name;

                 StackPanel panel = new StackPanel();
                 panel.Orientation = Orientation.Horizontal;
                 imgHeader = new Image();
                 imgHeader.Source = Utility.Envs.LoadImageSource(imageIndicator);
                 imgHeader.Width = 16;
                 imgHeader.Height = 16;
                 panel.Children.Add(imgHeader);
                 panel.Children.Add(new TextBlock() { Text = name, Margin = new System.Windows.Thickness(5, 0, 0, 0) });
                 this.Header = panel;
                 this.Name = index.ToString();
             }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="seqNo"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="parentId"></param>
        /// <param name="isSummary"></param>
        /// <param name="imageIndicator"></param>
        /// <param name="onBar"></param>
        public VTreeNode(int nodeId, int seqNo, string name, string description,
        int parentId, bool isSummary, string imageIndicator, bool onBar,Panels.IStartMenuTree handler,bool isEditable) 
        {
            _isEditable = isEditable;
            if (imageIndicator == null)
            {
                imageIndicator = "";

            }
            _Node_ID = nodeId;
            _seqNo = seqNo;
            _name = name;
            if (description == null)
                description = "";
            _description = description;
            _Parent_ID = parentId;
             _imageIndicator = imageIndicator;
             if (isSummary)
             {
                 imageIndicator = "K";
                 this.Expanded += new RoutedEventHandler(VTreeNode_Expanded);
                 this.Collapsed += new RoutedEventHandler(VTreeNode_Collapsed);
             }
             else
             {
                 this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(VTreeNode_MouseLeftButtonUp);
                ///////lakhwinder///////
                 if(string.IsNullOrEmpty(imageIndicator))
                 {
                    imageIndicator = "W"; 
                 }
                 ///////////////////
             }


             this.Header = GetHeaderPanel(imageIndicator, name);
            //this.Header = name;
            this.Name = nodeId.ToString();
            ToolTipService.SetToolTip(this, _description);
            
           

            if (isSummary == true) //Set Manually If summary node
            {
               // this.ImageKey = "C";
              // this.SelectedImageKey = "C";
            }
            else
            {
               // this.ImageKey = imageIndicator;
               // this.SelectedImageKey = imageIndicator;
            }
            SetSummary(isSummary);
            _onBar = onBar;

            _isFavorite = false;
            _handler = handler;
            
            //if(handler  == null)
            //{
            //    this.IsExpanded = true;
            //}

            SetContext();
            //_color = color;
        }

        //public VTreeNode(int nodeId, int seqNo, string name, string description,
        //int parentId, bool isSummary, string imageIndicator, bool onBar, Panels.IStartMenuTree handler):this
        //    (nodeId, seqNo,  name,  description,
        // parentId,  isSummary,  imageIndicator,  onBar,  handler,false)
        //{


        //}


        void VTreeNode_Collapsed(object sender, RoutedEventArgs e)
        {
            imgHeader.Source = Utility.Envs.LoadImageSource("folder.png");
            
        }

        void VTreeNode_Expanded(object sender, RoutedEventArgs e)
        {
            imgHeader.Source = Utility.Envs.LoadImageSource("folder-open.png");
        }

        Image imgHeader = null;

        private object GetHeaderPanel(string imageIndicator, string name)
        {
            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            imgHeader = new Image();
            imgHeader.Source = Utility.Envs.LoadImageSource(GetImageName(imageIndicator));
            imgHeader.Width = 16;
            imgHeader.Height = 16;
            panel.Children.Add(imgHeader);
            panel.Children.Add(new TextBlock() { Text = name, Margin = new System.Windows.Thickness(5, 0, 0, 0) });
            return panel;
        }

        void VTreeNode_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_handler != null)
            {
                _handler.ItemClick(this);
            }
        }

        public  string GetImageName(string initial)
        {
            int index = GetImageIndex(initial);


            switch (index)
            {
                case 1:
                    return "mWindow.png";
                case 2:
                    return "mReport.png";
                case 3:
                    return "mProcess.png";
                case 4:
                    return "mWorkflow.png";
                case 5:
                    return "mWorkbench.png";
                case 6:
                    return "mWindow.png";
                case 7:
                    return "mWindow.png";
                case 8:
                    return "mDocAction.png";
                case 10:
                    return "folder.png";
                default:
                    return "folder.png";
            }

            //imgList.Images.Add("D", VAdvantage.Properties.Resources.New161);
            //imgList.Images.Add("O", VAdvantage.Properties.Resources.House_3);
            //imgList.Images.Add("C", VAdvantage.Properties.Resources.folder);
            //imgList.Images.Add("B", VAdvantage.Properties.Resources.mWorkbench);
            //imgList.Images.Add("F", VAdvantage.Properties.Resources.mWorkFlow);
            //imgList.Images.Add("P", VAdvantage.Properties.Resources.mProcess);
            //imgList.Images.Add("R", VAdvantage.Properties.Resources.mReport);
            //imgList.Images.Add("T", VAdvantage.Properties.Resources.mUserChoice);
            //imgList.Images.Add("W", VAdvantage.Properties.Resources.mWindow);
            //imgList.Images.Add("X", VAdvantage.Properties.Resources.mWindow);

        }

        public  int GetImageIndex(String imageIndicator)
        {
            int imageIndex = 0;
            if (imageIndicator == null)
            {
            }
            else if (imageIndicator.Equals(ACTION_UserWindow)		//	Window 
                || imageIndicator.Equals(ACTION_UserForm))
                imageIndex = TYPE_WINDOW;
            else if (imageIndicator.Equals(ACTION_AppsReport))		//	Report
                imageIndex = TYPE_REPORT;
            else if (imageIndicator.Equals(ACTION_AppsProcess)		//	Process
                || imageIndicator.Equals(ACTION_AppsTask))
                imageIndex = TYPE_PROCESS;
            else if (imageIndicator.Equals(ACTION_SubWorkflow))		//	WorkFlow
                imageIndex = TYPE_WORKFLOW;
            /*
            else if (imageIndicator.Equals(MWFNode.ACTION_UserWorkbench))	//	Workbench
                imageIndex = TYPE_WORKBENCH;
            */
            else if (imageIndicator.Equals(ACTION_SetVariable))		//	Set Variable
                imageIndex = TYPE_SETVARIABLE;
            else if (imageIndicator.Equals(ACTION_UserChoice))		//	User Choice
                imageIndex = TYPE_USERCHOICE;
            else if (imageIndicator.Equals(ACTION_DocumentAction))	//	Document Action
                imageIndex = TYPE_DOCACTION;
            else if (imageIndicator.Equals(ACTION_WaitSleep))		//	Sleep
            {
            }
            else if (imageIndicator.Equals("K"))		//	Sleep
            {
                imageIndex = 10;
            }
            return imageIndex;
        }   //  getImage

        private bool _isFavorite = false;
        public bool IsFavorite
        {
            get { return _isFavorite; }
            set
            {
                _isFavorite = value;
                SetContext();
            }
        }


        /// <summary>
        /// copy Node Attribute(clone) 
        /// </summary>
        /// <param name="node">TreeNode</param>
        public VTreeNode(VTreeNode node,bool isFavorite, bool isLeafNode,Panels.IStartMenuTree handler) 
        {
            _isFavorite = isFavorite;   
            _Node_ID = node.Node_ID;
            //this.Name = isFavorite ?"f_" :"l_" + node.Node_ID.ToString();
            _seqNo = int.Parse(node.GetSeqNo());
            _name = node._name;
            

            this._imageIndex = node._imageIndex;
            //this.SelectedImageKey = node.SelectedImageKey;

            _description = node.GetDescription;
            ToolTipService.SetToolTip(this, node._description);

            _Parent_ID = node.Parent_ID;

            _isSummary = node.IsSummary;        /** Image Indicator				*/

            _imageIndicator = node._imageIndicator;

            _AD_Window_ID = node.AD_Window_ID;

            _AD_Process_ID = node.AD_Process_ID;

            _AD_Form_ID = node.AD_Form_ID;

            _AD_Workflow_ID = node.AD_Workflow_ID;

            _AD_Task_ID = node.AD_Task_ID;

            _AD_Workbench_ID = node.AD_Workflow_ID;

            _imageIndex = 0;
            /**	On Bar			*/
            _onBar = node.OnBar;

            this.Header = GetHeaderPanel(_imageIndicator, _name);

            if (!isLeafNode)
            {
                SetContext();
                this.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            }
            else
            {
                this.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                
                
            }
            this.MouseLeftButtonUp+=new System.Windows.Input.MouseButtonEventHandler(VTreeNode_MouseLeftButtonUp);
            _handler = handler;
        }

        /// <summary>
        /// Constructor copy node and its child nodes
        /// </summary>
        /// <param name="node"></param>
        /// <param name="copyChild"></param>
        public VTreeNode(VTreeNode node, bool copyChild)
        {

            _Node_ID = node.Node_ID;
            this.Name = node.Node_ID.ToString();
            _seqNo = int.Parse(node.GetSeqNo());
            _name = node.Name;
            this.Header = node.Header;

            //this.ImageKey = node.ImageKey;
            //this.SelectedImageKey = node.SelectedImageKey;

            _description = node.GetDescription;
            //this.ToolTipText = node.ToolTipText;

            _Parent_ID = node.Parent_ID;

            _isSummary = node.IsSummary;        /** Image Indicator				*/

            _imageIndicator = node._imageIndicator;

            _AD_Window_ID = node.AD_Window_ID;

            _AD_Process_ID = node.AD_Process_ID;

            _AD_Form_ID = node.AD_Form_ID;

            _AD_Workflow_ID = node.AD_Workflow_ID;

            _AD_Task_ID = node.AD_Task_ID;

            _AD_Workbench_ID = node.AD_Workflow_ID;

            _imageIndex = 0;
            /**	On Bar			*/
            _onBar = node.OnBar;

            
            //  TreeNodeCollection myTreeNodeCollection = node.Nodes;
            // Create an array of 'TreeNodes'.
            VTreeNode[] myTreeNodeArray = new VTreeNode[node.Items.Count];
            // Copy the tree nodes to the 'myTreeNodeArray' array.
            node.Items.CopyTo(myTreeNodeArray, 0);
            // Remove all the tree nodes from the 'myTreeViewBase' TreeView.

            try
            {
                node.Items.Clear();
                foreach (object o in myTreeNodeArray)
                {
                    this.Items.Add(o);
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// Constructor copy node and its child nodes
        /// </summary>
        /// <param name="node"></param>
        /// <param name="copyChild"></param>
        public VTreeNode(VTreeNode node, bool copyChild,bool copyEvent)
        {

            _Node_ID = node.Node_ID;
            this.Name = node.Node_ID.ToString();
            _seqNo = int.Parse(node.GetSeqNo());
            _name = node.Name;
            this.Header = node.Header;

            //this.ImageKey = node.ImageKey;
            //this.SelectedImageKey = node.SelectedImageKey;

            _description = node.GetDescription;
            //this.ToolTipText = node.ToolTipText;

            _Parent_ID = node.Parent_ID;

            _isSummary = node.IsSummary;        /** Image Indicator				*/

            _imageIndicator = node._imageIndicator;

            _AD_Window_ID = node.AD_Window_ID;

            _AD_Process_ID = node.AD_Process_ID;

            _AD_Form_ID = node.AD_Form_ID;

            _AD_Workflow_ID = node.AD_Workflow_ID;

            _AD_Task_ID = node.AD_Task_ID;

            _AD_Workbench_ID = node.AD_Workflow_ID;

            _imageIndex = 0;
            /**	On Bar			*/
            _onBar = node.OnBar;

            if (copyEvent)
            {
                this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(VTreeNode_MouseLeftButtonUp);
                _handler = node._handler;
            }
            //  TreeNodeCollection myTreeNodeCollection = node.Nodes;
            // Create an array of 'TreeNodes'.
            VTreeNode[] myTreeNodeArray = new VTreeNode[node.Items.Count];
            // Copy the tree nodes to the 'myTreeNodeArray' array.
            node.Items.CopyTo(myTreeNodeArray, 0);
            // Remove all the tree nodes from the 'myTreeViewBase' TreeView.

            try
            {
                node.Items.Clear();
                foreach (object o in myTreeNodeArray)
                {
                    this.Items.Add(o);
                }
            }
            catch
            {

            }
        }

        //Satandard Constructor
        public VTreeNode()
        {
            
        }

        private void SetContext()
        {
            if (!_isEditable)
            {
                if (this._isSummary)
                    return;


                if (contextMenu == null)
                {
                    contextMenu = new ContextMenu();
                    ContextMenuService.SetContextMenu(this, contextMenu);
                }

                contextMenu.Items.Clear();

                menuItem = new MenuItem();

                if (!_isFavorite)
                {
                    menuItem.Icon = "";
                    menuItem.Header = strAdd; //Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "AddToFavorite");
                }
                else
                {
                    menuItem.Icon = "";
                    menuItem.Header = strRemove;// Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "RemoveFromFavorite");
                }

                menuItem.Click += new System.Windows.RoutedEventHandler(menuItem_Click);
                contextMenu.Items.Add(menuItem);
            }

            else
            {
                if (this.IsSummary && this.Node_ID == 0)
                {
                    return;
                }

                if (contextMenu == null)
                {
                    contextMenu = new ContextMenu();
                    ContextMenuService.SetContextMenu(this, contextMenu);
                }

                contextMenu.Items.Clear();

                CopyItem = new MenuItem();

                //if (!_isFavorite)
                //{
                //    menuItem.Icon = "";
                //    menuItem.Header = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "AddToFavorite");
                //}
                //else
                //{
                //    menuItem.Icon = "";
                //    menuItem.Header = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "RemoveFromFavorite");
                //}


                CopyItem.Header = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "ItemMove");
                //CopyItem.Icon = Utility.Envs.LoadImageSource("move.png");
                contextMenu.Items.Add(CopyItem);

                CopyItem.Click += (s, e) =>
                    {
                        if (_handler != null)
                        {
                            _handler.ItemRightClick(this, ContextEventType.Move);

                        }
                    };

                
                PasteItem = new MenuItem();
                //PasteItem.Icon = Utility.Envs.LoadImageSource("Insert.png");
                PasteItem.Header = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "ItemInsert");
                PasteItem.Click += (ss, ee) =>
                    {
                        if (_handler != null)
                        {
                            _handler.ItemRightClick(this, ContextEventType.Insert);
                        }
                    };

                contextMenu.Items.Add(PasteItem);


                CancelItem = new MenuItem();
                CancelItem.Icon = Utility.Envs.LoadImageSource("Cancel16.png");
                CancelItem.Header = Utility.Msg.GetMsg(Utility.Envs.GetCtx(), "Cancel");
                CancelItem.Click += (ss, ee) =>
                {
                    if (_handler != null)
                    {
                        _handler.ItemRightClick(this, ContextEventType.Insert);
                    }
                };

                contextMenu.Items.Add(CancelItem);

                contextMenu.Closed += (sc, ec) =>
                    {
                        _handler.ItemContextEvent(this,true);

                    };
                contextMenu.Opened += (so, eo) =>
                    {
                        _handler.ItemContextEvent(this, false);
                    };
            }
        }

        void menuItem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!_isEditable)
            {
                if (!_isFavorite && _onBar)
                {
                    return;
                }

                if (_handler != null)
                {
                    _handler.ItemRightClick(this, _isFavorite);
                }
            }
        }

        #region Properties
        /// <summary>
        /// returm node id
        /// </summary>
        public int Node_ID
        {
            get
            {
                return _Node_ID;
            }
            set
            {
                _Node_ID = value;
            }
        }   //  getID

        /// <summary>
        /// returm SeqNo id
        /// </summary>
        public int SeqNo
        {
            get
            {
                return _seqNo;
            }
            set
            {
                _seqNo = value;
            }
        }

        /// <summary>
        /// get and set  window id
        /// </summary>
        public int AD_Window_ID
        {
            get
            {
                return _AD_Window_ID;
            }
            set
            {
                _AD_Window_ID = value;
            }
        }

        /// <summary>
        /// get and set process Id
        /// </summary>
        public int AD_Process_ID
        {
            get
            {
                return _AD_Process_ID;
            }
            set
            {
                _AD_Process_ID = value;
            }
        }

        /// <summary>
        /// get and set form id
        /// </summary>
        public int AD_Form_ID
        {
            get
            {
                return _AD_Form_ID;
            }
            set
            {
                this._AD_Form_ID = value;
            }
        }

        /// <summary>
        /// Get and set workflow id
        /// </summary>
        public int AD_Workflow_ID
        {
            get
            {
                return _AD_Workflow_ID;
            }
            set
            {
                this._AD_Workflow_ID = value;
            }
        }

        /// <summary>
        /// get and set task id
        /// </summary>
        public int AD_Task_ID
        {
            get
            {
                return _AD_Task_ID;
            }
            set
            {
                this._AD_Task_ID = value;
            }
        }

        /// <summary>
        /// Get and set workBench id
        /// </summary>
        public int AD_Workbench_ID
        {
            get
            {
                return _AD_Workbench_ID;
            }
            set
            {
                this._AD_Workbench_ID = value;
            }
        }
        /// <summary>
        /// get and set Parent id
        /// </summary>
        public int Parent_ID
        {
            get
            {
                return _Parent_ID;
            }
            set
            {
                _Parent_ID = value;
            }
        }
        /// <summary>
        /// true if node is on bar(favorite)
        /// </summary>
        /// <returns></returns>
        public bool OnBar
        {
            get
            {
                return _onBar;
            }
            set
            {
                _onBar = value;
            }

        }
        #endregion

        /// <summary>
        /// Get and set name of node
        /// </summary>
        public string SetName
        {
            set
            {
                if (value == null)
                    _name = "";
                else
                    _name = value;
               // this.Header = _name;

            }
            get
            {
                return _name;
            }
        }   //  setName

        public String GetName
        {
            get { return _name; }
        }

        public String GetImageIndicator
        {
            get { return _imageIndicator; }
        }

        private string colName;
        /// <summary>
        /// Get and set ColumnName of node
        /// </summary>
        public string ColName
        {
            get { return colName; }
            set { colName = value; }
        }

        //   /**
        //*  Set Summary (allow children)
        //*  @param isSummary true if summary
        //*/
        //   public void setAllowsChildren(bool isSummary)
        //   {
        //       super.setAllowsChildren(isSummary);
        //       m_isSummary = isSummary;
        //   }   //  setAllowsChildren


        /// <summary>
        ///Get SeqNo (Index) as formatted String 0000 for sorting
        /// </summary>
        /// <returns></returns>
        public string GetSeqNo()
        {
            string retValue = "0000" + _seqNo;	//	not more than 100,000 nodes
            if (_seqNo > 99999)
                if (retValue.Length > 5)
                    retValue = retValue.Substring(retValue.Length - 5);	//	last 5
            return retValue;
        }

        /// <summary>
        /// Get description 
        /// </summary>
        public string GetDescription
        {
            get
            {
                return _description;
            }
        }

        /// <summary>
        /// Set Summary property
        /// </summary>
        /// <param name="isSummary"></param>
        public void SetSummary(bool isSummary)
        {
            _isSummary = isSummary;

        }   //  setSummary

        /// <summary>
        /// true if summary node
        /// </summary>
        public bool IsSummary
        {
            get
            {
                return _isSummary;
            }
        }

        /// <summary>
        /// return true if process
        /// </summary>
        /// <returns></returns>
        public bool IsProcess()
        {
            return ACTION_Process.Equals(_imageIndicator);
        }	//	isProcess

        /**
         * 	Is Report
         *	@return true if report
         */
        public bool IsReport()
        {
            return ACTION_Report.Equals(_imageIndicator);
        }	//	isReport

        /**
         * 	Is Window
         *	@return true if Window
         */
        public bool IsWindow()
        {
            return ACTION_Window.Equals(_imageIndicator);
        }	//	isWindow

        /**
         * 	Is Workbench
         *	@return true if Workbench
         */
        public bool IsWorkbench()
        {
            return ACTION_Workbench.Equals(_imageIndicator);
        }	//	isWorkbench

        /**
         * 	Is Workflow
         *	@return true if Workflow
         */
        public bool IsWorkFlow()
        {
            return ACTION_WorkFlow.Equals(_imageIndicator);
        }	//	isWorkFlow

        /**
         * 	Is Form
         *	@return true if Form
         */
        public bool IsForm()
        {
            return ACTION_Form.Equals(_imageIndicator);
        }	//	isForm

        /**
         * 	Is Task
         *	@return true if Task
         */
        public bool IsTask()
        {
            return ACTION_Task.Equals(_imageIndicator);
        }	//	isTask


        // imageIndicator image indicator (W/X/R/P/F/T/B)
        //  Get Node ID
        //  @return node id (e.g. AD_Menu_ID)

        public int GetNode_ID()
        {
            return Node_ID;// node_ID;
        }

        /**	Last found ID				*/
        private int _lastID = -1;
        /** Last found Node				*/
        private VTreeNode _lastNode = null;

        /// <summary>
        ///Return the Node with ID in list of children
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public VTreeNode FindNode(int ID)
        {
            if (_Node_ID == ID)
                return this;
            ////
            if (ID == _lastID && _lastNode != null)
                return _lastNode;

            object o =  this.FindName(ID.ToString());

            ////
           // VTreeNode[] node = this.Items.GetValue((ID.ToString(), true);
            //if (node.GetLength(0) > 0)
            //{
            //    _lastID = ID;
            //    _lastNode = (VTreeNode)node[0];
            //    return _lastNode;

            //}
            if (o != null && o is TreeViewItem)
            {
                return o as VTreeNode;
            }
            return null;
        }

        public override string ToString()
        {
            return _name;
        }

        public IEnumerator preorderEnumeration()
        {
            return new PreorderEnumerator(this);
        }

        public string ToolTipText
        {
            set
            {
                ToolTipService.SetToolTip(this, value);
            }
        }

        public string ImageKey
        {
            set
            {
                imgHeader.Source = Utility.Envs.LoadImageSource(GetImageName(value));
            }
        }
    }

    public class PreorderEnumerator : System.Collections.Generic.IEnumerator<object>
    {

        protected  List<TreeViewItem> stack;

        protected TreeViewItem root = null;

        private bool _isLast = false;
        public bool isLast
        {
            get
            {
                return _isLast;
            }
        }

        public PreorderEnumerator(TreeViewItem rootNode)
        {
            //Vector v = new Vector(1);
            //v.addElement(rootNode);	// PENDING: don't really need a vector
            //stack = new Stack();
            //stack.Push(rootNode);
            stack = new List<TreeViewItem>();
            root = rootNode;
            stack.Add(rootNode);
        }

        //public bool HasMoreElements()
        //{
        //    return (stack.Count != 0 && (((TreeNode)stack.Peek()).Nodes.Count > 0));    //hasMoreElements());
        //}
        IEnumerator children;
        public object Current
        {
            //  IEnumerator enumer = (IEnumerator)stack.Peek();\
            get
            {
                //IEnumerator enumer = (IEnumerator)stack.Peek();

                TreeViewItem node = stack[stack.Count - 1];

                ItemCollection nodes = node.Items;
                //TreeNodeCollection nodes = node.Nodes; ;

                //if (node.Items.Count > 0)
                //{
                //    //children = node.Nodes.GetEnumerator();
                //    nodes = node.Items;
                //}
                //else
                //{
                stack.Remove(node);
                    for (int i = nodes.Count - 1; i >= 0; i--)
                    {
                        //stack.Push(children.Current);
                        stack.Add((TreeViewItem)nodes[i]);
                    }
                //}
                    _isLast = stack.Count == 1 && stack[0].Items.Count==0;
                    return node;
            }
        }
        // Summary:
        //     Advances the enumerator to the next element of the collection.
        //
        // Returns:
        //     true if the enumerator was successfully advanced to the next element; false
        //     if the enumerator has passed the end of the collection.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        //     The collection was modified after the enumerator was created.
        public bool MoveNext()
        {
            return stack.Count != 0; //&& (((TreeNode)stack.Peek()).Nodes.Count > 0)    //hasMoreElements());
        }
        //
        // Summary:
        //     Sets the enumerator to its initial position, which is before the first element
        //     in the collection.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        //     The collection was modified after the enumerator was created.
        public void Reset()
        {
            if (root != null)
            {
                stack.Clear();
                _isLast = false;
                stack.Add(root);
            }
        }
        public void Dispose()
        {
            if (stack != null)
                stack.Clear();
            stack = null;
            root = null;
        }

        //public TreeNode NextElement()
        //{
        //    IEnumerator enumer = (IEnumerator)stack.Peek();
        //    TreeNode node = (TreeNode)enumer.Current;
        //    IEnumerator children = node.Nodes;

        //    if (!enumer.MoveNext())
        //    {
        //        stack.Pop();
        //    }
        //    if (children.MoveNext())
        //    {
        //        stack.Push(children);
        //    }
        //    return node;
        //}
    }

    public class SearchItem
    {
        public String Name
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public BitmapImage ImageSource
        {
            get;
            set;
        }

        public int ID
        {
            get;
            set;
        }

        public SearchItem(VTreeNode node)
        {
            Name = node.GetName;
            Description = node.GetDescription;
            ImageSource = Utility.Envs.LoadImageSource(node.GetImageName(node.GetImageIndicator));
            ID = node.GetNode_ID();
        }
    }


   
}

